package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.SmsHomeRecommendProduct;
import com.itheima.mapper.SmsHomeRecommendProductMapper;
import com.itheima.service.db.SmsHomeRecommendProductService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class SmsHomeRecommendProductServiceImpl implements SmsHomeRecommendProductService {
    @Autowired
    SmsHomeRecommendProductMapper smsHomeRecommendProductMapper;
    @Override
    public void save(SmsHomeRecommendProduct RecommendProduct) {
        smsHomeRecommendProductMapper.insert(RecommendProduct);
    }

    @Override
    public void deleteRecommendProduct(List<Long> ids) {
        smsHomeRecommendProductMapper.deleteBatchIds(ids);
    }

    @Override
    public PageBeanVo findByPage(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        Page<SmsHomeRecommendProduct> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<SmsHomeRecommendProduct> qw = new LambdaQueryWrapper<>();

        qw.like(StrUtil.isNotEmpty(productName), SmsHomeRecommendProduct::getProductName, productName);
        qw.eq(recommendStatus != null,SmsHomeRecommendProduct::getRecommendStatus, recommendStatus);
        qw.orderByAsc(SmsHomeRecommendProduct::getSort);

        Page<SmsHomeRecommendProduct> smsHomeRecommendProductPage = smsHomeRecommendProductMapper.selectPage(page, qw);

        return new PageBeanVo(pageNum,pageSize,smsHomeRecommendProductPage.getTotal(),smsHomeRecommendProductPage.getRecords());
    }

    @Override
    public void updateRecommendProduct(Long id, Integer recommendStatus) {
        SmsHomeRecommendProduct RecommendProduct = new SmsHomeRecommendProduct();
        RecommendProduct.setId(id);
        RecommendProduct.setRecommendStatus(recommendStatus);
        smsHomeRecommendProductMapper.updateById(RecommendProduct);
    }

    @Override
    public void updateSortRecommendProduct(Long id, Integer sort) {
        SmsHomeRecommendProduct RecommendProduct = new SmsHomeRecommendProduct();
        RecommendProduct.setId(id);
        RecommendProduct.setSort(sort);
        smsHomeRecommendProductMapper.updateById(RecommendProduct);
    }
}
